<template>
  <div class="payment-success-page">
    <!-- 背景装饰 -->
    <div class="background-effects">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
    </div>

    <div class="page-header">
      <h1>支付成功</h1>
    </div>

    <el-card class="success-card">
      <!-- 成功动画 -->
      <div class="success-animation">
        <div class="checkmark-container">
          <svg class="checkmark" viewBox="0 0 52 52">
            <circle class="checkmark__circle" cx="26" cy="26" fill="none" r="25"/>
            <path class="checkmark__check" d="M14.1 27.2l7.1 7.2 16.7-16.8" fill="none"/>
          </svg>
        </div>
      </div>

      <div class="success-content">
        <!-- 成功信息 -->
        <div class="success-message">
          <h2>支付成功！</h2>
          <p class="subtitle">订单已支付成功，卖家将尽快为您处理</p>

          <!-- 订单信息卡片 -->
          <div class="order-info-card">
            <div class="info-section">
              <h3>订单信息</h3>
              <div class="info-grid">
                <div class="info-item">
                  <span class="label">订单编号：</span>
                  <span class="value">{{ orderInfo.orderSn || orderInfo.orderId }}</span>
                </div>
                <div class="info-item">
                  <span class="label">支付金额：</span>
                  <span class="value amount">{{ orderInfo.amount }}</span>
                </div>
                <div class="info-item">
                  <span class="label">支付时间：</span>
                  <span class="value">{{ currentTime }}</span>
                </div>
              </div>
            </div>

            <!-- 商品信息 -->
            <div v-if="orderInfo.productName" class="info-section">
              <h3>商品信息</h3>
              <div class="product-info">
                <div class="product-image">
                  <el-image
                      :src="orderInfo.productImage || '/default-product.png'"
                      fit="cover"
                  ></el-image>
                </div>
                <div class="product-details">
                  <h4>{{ orderInfo.productName }}</h4>
                  <p class="product-price">{{ orderInfo.amount }}</p>
                </div>
              </div>
            </div>

            <!-- 收货信息 -->
            <div v-if="orderInfo.consignee" class="info-section">
              <h3>收货信息</h3>
              <div class="info-grid">
                <div class="info-item">
                  <span class="label">收货人：</span>
                  <span class="value">{{ orderInfo.consignee }}</span>
                </div>
                <div class="info-item">
                  <span class="label">联系电话：</span>
                  <span class="value">{{ orderInfo.phone || '未填写' }}</span>
                </div>
                <div class="info-item full-width">
                  <span class="label">收货地址：</span>
                  <span class="value">{{ orderInfo.fullAddress }}</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 倒计时提示 -->
          <div class="countdown-tips">
            <p>
              <span class="countdown">{{ countdown }}</span> 秒后自动跳转到订单页面
            </p>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="action-buttons">
          <el-button class="action-btn" size="large" type="primary" @click="viewOrder">
            <el-icon>
              <View/>
            </el-icon>
            查看订单详情
          </el-button>
          <el-button class="action-btn" size="large" @click="backToHome">
            <el-icon>
              <HomeFilled/>
            </el-icon>
            返回首页
          </el-button>
          <el-button class="action-btn" size="large" @click="continueShopping">
            <el-icon>
              <ShoppingBag/>
            </el-icon>
            继续购物
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, onUnmounted, reactive, ref} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {HomeFilled, ShoppingBag, View} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 响应式数据
const countdown = ref(10)
const currentTime = ref('')
const timer = ref(null)

// 订单信息
const orderInfo = reactive({
  orderId: '',
  orderSn: '',
  amount: '',
  productName: '',
  productImage: '',
  consignee: '',
  phone: '',
  fullAddress: ''
})

// 获取当前时间
const getCurrentTime = () => {
  const now = new Date()
  return now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 查看订单详情
const viewOrder = () => {
  clearTimeout(timer.value)
  router.push({
    name: 'OrderDetail',
    params: {id: orderInfo.orderId},
    query: {role: 'buyer'}
  })
}

// 返回首页
const backToHome = () => {
  clearTimeout(timer.value)
  router.push('/userHome/showGoodList')
}

// 继续购物
const continueShopping = () => {
  clearTimeout(timer.value)
  router.push('/userHome/showGoodList')
}

// 开始倒计时
const startCountdown = () => {
  timer.value = setInterval(() => {
    if (countdown.value > 1) {
      console.log(orderInfo)
      countdown.value--
    } else {
      clearTimeout(timer.value)
      viewOrder()
    }
  }, 1000)
}

// 组件挂载时的逻辑
onMounted(() => {
  // 设置当前时间
  currentTime.value = getCurrentTime()
  // 从路由参数中获取订单信息
  orderInfo.orderId = route.query.orderId
  orderInfo.orderSn = route.query.orderSn
  orderInfo.amount = route.query.amount
  orderInfo.productName = route.query.productName
  orderInfo.productImage = route.query.productImage
  orderInfo.consignee = route.query.consignee
  orderInfo.phone = route.query.phone
  orderInfo.fullAddress = route.query.fullAddress


  // 开始倒计时
  startCountdown()
})

// 组件卸载时清除定时器
onUnmounted(() => {
  if (timer.value) {
    clearTimeout(timer.value)
  }
})
</script>

<style scoped>
.payment-success-page {
  padding: 20px;
  position: relative;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.background-effects {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 100px;
  height: 100px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 80px;
  height: 80px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.page-header {
  margin-bottom: 30px;
  text-align: center;
  position: relative;
  z-index: 1;
}

.page-header h1 {
  margin: 0;
  font-size: 28px;
  color: #2c3e50;
  font-weight: 600;
}

.success-card {
  max-width: 800px;
  margin: 0 auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  border: none;
  border-radius: 16px;
  overflow: hidden;
  position: relative;
  z-index: 1;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

.success-animation {
  display: flex;
  justify-content: center;
  padding: 40px 0 20px;
}

.checkmark-container {
  width: 80px;
  height: 80px;
}

.checkmark {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: block;
  stroke-width: 2;
  stroke: #4CAF50;
  stroke-miterlimit: 10;
  animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both;
}

.checkmark__circle {
  stroke-dasharray: 166;
  stroke-dashoffset: 166;
  stroke-width: 2;
  stroke-miterlimit: 10;
  stroke: #4CAF50;
  fill: none;
  animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}

.checkmark__check {
  transform-origin: 50% 50%;
  stroke-dasharray: 48;
  stroke-dashoffset: 48;
  animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards;
}

@keyframes stroke {
  100% {
    stroke-dashoffset: 0;
  }
}

@keyframes scale {
  0%, 100% {
    transform: none;
  }
  50% {
    transform: scale3d(1.1, 1.1, 1);
  }
}

@keyframes fill {
  100% {
    box-shadow: inset 0px 0px 0px 30px #4CAF50;
  }
}

.success-content {
  padding: 0 40px 40px;
}

.success-message {
  text-align: center;
  margin-bottom: 40px;
}

.success-message h2 {
  font-size: 32px;
  color: #4CAF50;
  margin-bottom: 12px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 18px;
  margin-bottom: 40px;
}

.order-info-card {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 30px;
  margin-bottom: 30px;
  text-align: left;
}

.info-section {
  margin-bottom: 30px;
}

.info-section:last-child {
  margin-bottom: 0;
}

.info-section h3 {
  color: #2c3e50;
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: 600;
  border-left: 4px solid #4CAF50;
  padding-left: 12px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.info-item {
  display: flex;
}

.info-item.full-width {
  grid-column: 1 / -1;
}

.label {
  color: #666;
  font-weight: 500;
  min-width: 100px;
}

.value {
  color: #2c3e50;
  font-weight: 500;
}

.value.amount {
  color: #e74c3c;
  font-size: 18px;
  font-weight: 600;
}

.product-info {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 16px;
  background: white;
  border-radius: 8px;
  border: 1px solid #eaeaea;
}

.product-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
}

.product-image :deep(.el-image) {
  width: 100%;
  height: 100%;
}

.product-details h4 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  font-size: 16px;
}

.product-price {
  color: #e74c3c;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.countdown-tips {
  margin-top: 30px;
  padding: 16px;
  background: #e8f5e8;
  border-radius: 8px;
  border: 1px solid #4CAF50;
}

.countdown-tips p {
  margin: 0;
  color: #2c3e50;
  font-size: 16px;
}

.countdown {
  color: #e74c3c;
  font-weight: 600;
  font-size: 18px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.action-btn {
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@media (max-width: 768px) {
  .payment-success-page {
    padding: 16px;
  }

  .success-content {
    padding: 0 20px 30px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .action-buttons {
    flex-direction: column;
  }

  .action-btn {
    width: 100%;
  }

  .product-info {
    flex-direction: column;
    text-align: center;
  }
}
</style>